package com.winchain.chainbill.tbill.biz.service.impl;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.base.po.TbSnd;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.BillAcctService;
import com.winchain.chainbill.trade.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TBillQuotaService {
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;
	@Autowired
	private TradeService tradeService;

	public void trade(TbRec bill, TbSnd send, String optType, String remark) {
		String bizType = "dct_apply";
		String acctType = "1";
		AcctTxRec tx = getTxRec(bill, send, bizType, optType, acctType, remark);

		tx.setAcctin(false);
		this.tradeService.callback(tx);
	}

	private AcctTxRec getTxRec(TbRec bill, TbSnd send, String bizType, String optType, String acctType, String remark) {
		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(txRec.getTxId());
		txRec.setTxAmt(bill.getBillAmt());
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs(remark);
		txRec.setTxType(bizType);
		txRec.setOptType(optType);
		txRec.setTxSts("S");
		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(bill.getBillId());
		txRec.setDataScLvl("B");
		txRec.setCurrCode(AssetCode.XTB.name());
		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");
		txRec.setTxOperator(CommonDataUtil.getCurEntId());

		AcctBas acct = this.billAcctService.getAcctBas(send.getBuyerId(), acctType, null);
		txRec.setTxAcctNo(acct.getAcctNo());
		txRec.setTxAcctNm(acct.getAcctNm());
		txRec.setTxBankNm(acct.getFiNm());

		AcctBkp bkp = this.billAcctBlcService.getCreditAcctBkpByCont(bill.getFinId(), send.getBuyerId(),
				AssetCode.XTB.name(), "41");
		txRec.setTxBkpId(bkp.getBkpId());
		return txRec;
	}
}
